package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.PercentageItem;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.getAgentConditionMarkupConfigDetailBean;
import com.ymatou.iapi.godmch.parameter.setAgentConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setAgentMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getAgentConditionMarkupConfigDetailCall;
import com.ymatou.iapi.godmch.service.setAgentConditionMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_agent_priceWapper;
/**
*代理价按条件加价规则详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentConditionMarkupConfigDetail{
	private static getAgentConditionMarkupConfigDetailBean bean;
	private static getAgentConditionMarkupConfigDetailCall call;
	markup_config_agent_priceWapper priceWapper=new markup_config_agent_priceWapper();
	agent_typeWapper agentTypeWapper = new agent_typeWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理价按条件加价规则详情");
	}

	@Before
	public void caseUp() {
		bean=new getAgentConditionMarkupConfigDetailBean();
		call=new getAgentConditionMarkupConfigDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	private void check_verify(setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean) throws InterruptedException {

		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(setAgentConditionMarkupConfigBean.getMchId(), call.getResult().getInt("mchId"), "验证mchId");
		Logger.verifyEquals(setAgentConditionMarkupConfigBean.getDecimalType(), call.getResult().getInt("decimalType"), "验证decimalType");
		Logger.verifyEquals(setAgentConditionMarkupConfigBean.getRefreshProduct(), call.getResult().getBoolean("refreshProduct"), "验证refreshProduct");
		
		List<PercentageItem> return_Items=call.getPercentageList();
		for(int i=0;i<return_Items.size();i++)
		{
			PercentageItem percentageItem=setAgentConditionMarkupConfigBean.getPercentageList().get(i);
			Logger.verifyEquals(return_Items.get(i).getPercentage(), percentageItem.getPercentage(), "验证Percentage");
			Logger.verifyEquals(return_Items.get(i).getAgentTypeId(), percentageItem.getAgentTypeId(), "验证AgentTypeId");
			
	        //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", setAgentConditionMarkupConfigBean.getMchId());
			map.put("agentType", percentageItem.getAgentTypeId());
			agent_type DB_type = agentTypeWapper.selectby(map).get(0);
			Logger.verifyEquals(return_Items.get(i).getAgentTypeTitle(), DB_type.getTypeTitle(), "验证AgentTypeTitle");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_001() {
		Logger.start(true,"代理价按条件加价规则详情-商户和代理类型存在的-返回成功");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);

			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();			
			
			check_verify(setAgentConditionMarkupConfigBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_002() {
		Logger.start(true,"代理价按条件加价规则详情-商户有两个代理-返回成功");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			//创建代理类型
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(setAgentConditionMarkupConfigBean.getMchId());
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			//查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", setAgentConditionMarkupConfigBean.getMchId());
			List<agent_type> list = agentTypeWapper.selectby(map);
			//代理类型1
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			//代理类型2
			item=new PercentageItem();
			item.setAgentTypeId(list.get(1).getAgentType());
			item.setPercentage(new BigDecimal(20));
			items.add(item);
			setAgentConditionMarkupConfigBean.setPercentageList(items);
			
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			//获取MarkupId
			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();			
			
			check_verify(setAgentConditionMarkupConfigBean);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_003() {
		Logger.start(true,"代理价按条件加价规则详情-商户id不存在-返回失败");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);

			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId()+111);
			bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_004() {
		Logger.start(true,"代理价按条件加价规则详情-markupId不存在-返回失败");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);

			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setMarkupId(agentPrice.getMarkup_id()+111);
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_005() {
		Logger.start(true,"代理价按条件加价规则详情-mchId与markupId关系不存在-返回失败");
		try {
			//代理价按条件加价规则1
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			Thread.sleep(5000);
			//代理价按条件加价规则2
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean2=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean2);

			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			
			bean.setMchId(setAgentConditionMarkupConfigBean2.getMchId());
			bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_006() {
		Logger.start(true,"代理价按条件加价规则详情-商户id为空-返回失败");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			
			markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
			priceExample.createCriteria().andMch_idEqualTo(setAgentConditionMarkupConfigBean.getMchId());
			markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
			//bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigDetail_007() {
		Logger.start(true,"代理价按条件加价规则详情-markupId为空-返回失败");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			//bean.setMarkupId(agentPrice.getMarkup_id());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}